<template>
	<view class="test">
		<g-navbar title="TEST"></g-navbar>
		<uni-segmented-control :current="current" :values="items" style-type="button" active-color="#365FFF"
			style="margin-top: 50rpx;" @clickItem="onClickItem" />
		<uni-segmented-control :current="currentBaseurl" :values="baseUrlList.map(item=>item.label)" style-type="button"
			active-color="#365FFF" style="margin-top: 50rpx;" @clickItem="onClickBaseurlItem" />
		<g-button btn_text="微信授权，生成code" @click="global.authorization('/pages/test/test')"
			style="margin-top: 50rpx;"></g-button>
		<view style="margin-top: 50rpx;">
			<text style="color: red;">wx_code</text>:{{wx_code}} <i class="iconfont icon-copy" v-if="wx_code"
				@click="copy" />
		</view>
		<view style="width: 200rpx;border: 1px red solid;color:red;fontSize:20rpx">
			<g-auto-zoom-text>测试文字测试文字测试文字测试文字测试文字测试文字测试文字</g-auto-zoom-text>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { onLoad, onShow } from '@dcloudio/uni-app';
	import { ref, Ref } from "vue";
	import { useStore } from 'vuex';
	import global from '@/global';

	// onLoad((options) => {
	// 	console.log('options===>',options)
	// 	if (options && options.code) {
	// 		// #ifdef H5
	// 		wxLoginH5(options.code);
	// 		// #endif
	// 	}
	// })

	const store = useStore();

	const current : Ref<number> = ref(store.state.version - 1);



	const items = ['红包系统', '优创系统']

	const baseUrlList = [{ label: '测试接口', value: 'https://wechat.huazhanhao.top/api' }, { label: '正式接口', value: 'https://redpack.gdlzjt.com/api' }];

	const currentBaseurl : Ref<number> = ref(store.state.baseUrl === baseUrlList[0].value ? 0 : 1);

	onShow(() => {
		const searchParams = new URL(window.location.href).searchParams;
		const params = Object.fromEntries(searchParams);
		console.log(params)
		wx_code.value = params.code;
	})

	const wx_code : Ref<string> = ref('');

	const authorization = () => {

		// http://localhost:5173/#/pages_login/login_entry/loginEntry
		// 微信授权登录
		let redirect_uri = encodeURIComponent(`${store.state.website}/pages/test/test`),
			appid = "wx44b35d013a63718d";
		let wx_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
		window.location.href = wx_url;
	}

	const wxLoginH5 = (code : string) => {
		console.log('微信登录，H5', code);
		wx_code.value = code;
	}

	const copy = () => {
		uni.setClipboardData({
			data: wx_code.value,
			success: () => {
				uni.prompt.$showToast('已复制code')
			}
		});
	}

	const onClickItem = (e) => {
		uni.store.local('version', e.currentIndex + 1);
	}

	const onClickBaseurlItem = (e) => {
		console.log(e)
		uni.store.local('baseUrl',baseUrlList[e.currentIndex].value);
		console.log(store.state.baseUrl)
	}
</script>

<style scoped lang="scss">
	.test {
		padding: 32rpx;
	}
</style>